#include "log.h"
#include "stdio.h"

#include <string>
#include <ctime>

using namespace std;

FILE *fileHandle;

/**
 * Opens the log file and creates a new Log instance.
 * @param	file		Name of the log file (omav.name.log).
 */
Log::Log(string file) {
	// Determine the file path
	string filePath = "/home/mark/omav.";
	filePath.append(file);
	filePath.append(".log");

	char *cFilePath = (char*)filePath.c_str();

	// Open the file in append mode
	fileHandle = fopen(cFilePath, "a");
}

/**
 * Closes the file and destroys the Log instance.
 */
Log::~Log() {
	// Close the file
	fclose(fileHandle);
}

/**
 * Writes data to the log file.
 * @param	type		Type of message.
 * @param	message		Data to write to the log file.
 */
void Log::Write(string type, string message) {
	// Create the time buffers
	time_t		now			=	time(0);
	struct	tm	tstruct;
	char		timeBuffer		[80];

	// Get the current time and format it
	tstruct = *localtime(&now);
	strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d\t%H:%M:%S", &tstruct);

	// Convert string to char arrays
	char *c_type = (char*)type.c_str();
	char *c_message = (char*)message.c_str();

	// Append to the log file
	fprintf(fileHandle, "%s\t%s\t%s\n", timeBuffer, c_type, c_message);
}

/**
 * Writes an information entry to the log file.
 * @param	message		Information message.
 */
void Log::WriteInformation(string message) {
	this->Write("information", message);
}

/**
 * Writes a warning entry to the log file.
 * @param	message		Warning message.
 */
void Log::WriteWarning(string message) {
	this->Write("warning\t", message);
}

/**
 * Writes an error entry to the log file.
 * @param	message		Error message.
 */
void Log::WriteError(string message) {
	this->Write("error\t", message);
}



















